<?php
class News_Model_Item extends Zen_Item 
{
	public function __construct() {
		$this->table = new Zend_Db_Table('news');
		$this->attachBehavior(new Zen_Behavior_Weight($this))
			 ->attachBehavior(new Zen_Behavior_Alias($this));
	}

	public function getList( $page=1, $limit=0, $clause=null ){
		$date = date ( 'Y-m-d' );
		$where = "`main`=1 and  `active`=1 and ((`date_start`<='" . $date . "') or `date_start` is NULL ) and ((`date_end` >='" . $date . "') or `date_end` is NULL )";
		if ( $clause )	$where .= "and $clause";
		$select = $this->select()->setPage($page)->setLimit($limit)
						->setPaging(true)->setOrder('date desc');
		$res = $select->getData($where);
	 	$this->_pagecount = $select->getPagecount();
		return $res;
	}
	
	public function getPageCount(){
		return ( $this->_pagecount ) ? $this->_pagecount : 1;
	}
	
	public function getBlock() {
		return $this->getList(1, 20, $where);
	}
	
	public function getOne($id) {
		return $this->select()->getOne("active=1 and id='$id'");
	}
	
	public function getYears() {
		$date = date ( 'Y-m-d' );
		$dates=$this->getTable()->select()->distinct()->from($this->getTable(),array('date'))->order('date DESC')->query()->fetchAll();
		if($dates){
			$years=array();
			foreach ($dates as $date){
				$date = new Zend_Date($date['date'],'yyyy-MM-dd');
				$date->setLocale('ru_RU');	
				$years[$date->toString('yyyy')]=$date->toString('yyyy');
			}
		}
		return $years;
	}
	
	public function getYear($year) {
		$years = $this->getYears();
		if (! $years) 
			return false;
		if (! $year)
			reset($years);
			return current($years);
		if (isset($years[$year]))
			return $year;
		return false;
	}
		
}